package model;


import java.sql.Statement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

import javax.swing.text.StyledEditorKit.BoldAction;

import Entity.*;
import S2.server.ConnectionManage;


public class CustomerModel {

	private static Connection conn = ConnectionManage.getInstance()
			.getConnection();


	public static boolean addCustomer(String Id,String name, String birthDate, String rate ) throws SQLException 
	{
		
		String sql = "insert into customer (name, birth_date) "+" values (?, ?)";
		ResultSet rs = null;
		Customer bean =  new Customer();


		try (PreparedStatement stmt = conn.prepareStatement(sql);) {
			stmt.setString(1, name);
			stmt.setString(2, birthDate);
	//		stmt.setString(3 , rate);
			
		
			boolean res = stmt.execute();
			return !res;

	

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			return false;
		} finally {
			if (rs != null) {
				rs.close();
			}
		}
//		return true;
	}
	public static Customer UpdateCustomer(String Id,String name, String birthDate ,String rate) throws SQLException 
	{
		String sql = "Update  customer(name,age,rate)"+"VALUES(?,?,?,?)";
		ResultSet rs = null;
		Customer bean =  new Customer();
		
		try (PreparedStatement stmt = conn.prepareStatement(sql);) {
		 
		    stmt.setString(1, Id);
			stmt.setString(2, name);
			stmt.setString(3, birthDate);
			stmt.setString(4 , rate);
			
			
			rs = stmt.executeQuery();
			if (rs.next()) {
				bean.setName(name);
				bean.setId(Id);
				bean.setbirthDate(birthDate);
				bean.setRate(rate);
			}
	}
		 catch (SQLException e) {
				System.err.println(e);
				return null;
			} finally {
				if (rs != null) {
					rs.close();
				}
			
		
	}
		return bean;
}
public static void DeleteCustomer(String name,String CustomerId)throws SQLException 
{
	
	String sql = "DELETE FROM Customer " +  "WHERE name = ?" + "AND Id =?";
	ResultSet rs = null;
	
	try (PreparedStatement stmt = conn.prepareStatement(sql);)
	{		
		stmt.setString(1, name);
		stmt.setString(2, CustomerId);
		rs = stmt.executeQuery();
	}
	 catch (SQLException e) {
			System.err.println(e);
			
		} finally {
			if (rs != null) {
				rs.close();
			}
		
	
		}
}
public static Customer getCustomer(String name, String Id ) throws SQLException
{
	String sql = "SELECT * FROM" + " Customer WHERE" + " name=? AND Id =? ";
	ResultSet rs = null;
	Customer bean =  new Customer();

	try (PreparedStatement stmt = conn.prepareStatement(sql);) {
		stmt.setString(1, name);
		stmt.setString(2,Id);
	
		rs = stmt.executeQuery();
		
		if (rs.next()) {
			bean.setName(name);
			bean.setId(Id);
		
			return bean;
		} else {
			return null;
		}

	} catch (SQLException e) {
		System.err.println(e);
		return null;
	} finally {
		if (rs != null) {
			rs.close();
		}
	}
}
}
